//
//  ST_PeculiarityModuleViewController.swift
//  btvideo
//
//  Created by mac on 2020/7/20.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyUserDefaults

/**
 特质模块
 */
class ST_PeculiarityModuleViewController: UIViewController{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        
        
        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: statusBarHeight + 54, left: 0, bottom: 0, right: 0))
        }
        
        scrollView.addSubview(root_view)
        
        let h = ScreenHeight - statusBarHeight - 54
        
        root_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
            make.width.equalTo(ScreenWidth*3)
            make.height.equalTo(h)
        }
        
        
        
        self.addChild(recommend_controller)
        self.addChild(peculiarity_squre_controller)
        self.addChild(my_follow_controller)
        
        
        root_view.addSubview(my_follow_controller.view)
        root_view.addSubview(recommend_controller.view)
        root_view.addSubview(peculiarity_squre_controller.view)
        
        
        my_follow_controller.view.snp.makeConstraints { (make) in
            make.width.equalTo(ScreenWidth)
            make.height.equalTo(h)
            make.top.bottom.left.equalToSuperview()
        }
        
        recommend_controller.view.snp.makeConstraints { (make) in
            make.width.equalTo(ScreenWidth)
            make.height.equalTo(h)
            make.top.bottom.equalToSuperview()
            make.left.equalTo(my_follow_controller.view.snp.right)
        }
        
        peculiarity_squre_controller.view.snp.makeConstraints { (make) in
            make.width.equalTo(ScreenWidth)
            make.height.equalTo(h)
            make.top.bottom.equalToSuperview()
            make.left.equalTo(recommend_controller.view.snp.right)
        }
        
        
        self.view.addSubview(recommend_tab_btn)
        self.view.addSubview(follow_tab_btn)
        self.view.addSubview(type_tab_btn)
        
        //        view.addSubview(sign_label)
        //        sign_label.snp.makeConstraints { (make) in
        //            make.right.top.equalTo(recommend_tab_btn)
        //            make.width.equalTo(29)
        //            make.height.equalTo(16)
        //        }
        
        let relativeOffset = 10/375*ScreenWidth
        
        recommend_tab_btn.snp.makeConstraints { (make) in
            make.width.equalTo(76)
            make.height.equalTo(38)
            make.top.equalToSuperview().offset(statusBarHeight+3)
            make.centerX.equalToSuperview()
        }
        
        follow_tab_btn.snp.makeConstraints { (make) in
            make.width.equalTo(76)
            make.height.equalTo(38)
            make.top.equalToSuperview().offset(statusBarHeight+3)
            make.right.equalTo(recommend_tab_btn.snp.left).offset(-relativeOffset)
        }
        
        
        type_tab_btn.snp.makeConstraints { (make) in
            make.width.equalTo(80)
            make.height.equalTo(38)
            make.top.equalToSuperview().offset(statusBarHeight+3)
            make.left.equalTo(recommend_tab_btn.snp.right).offset(relativeOffset)
        }
        
        let offset:CGFloat = (44-30)/2.0
        
        self.view.addSubview(add_btn)
        add_btn.snp.makeConstraints { (make) in
            make.width.height.equalTo(44)
            make.right.equalToSuperview().offset(-15+offset)
            make.top.equalToSuperview().offset(statusBarHeight+7-offset)
        }
        
        /*
         self.view.addSubview(filter_btn)
         filter_btn.snp.makeConstraints { (make) in
         make.width.height.equalTo(44)
         make.left.equalToSuperview().offset(15-offset)
         make.top.equalToSuperview().offset(statusBarHeight+7-offset)
         }*/
        
        
        self.view.addSubview(modify_love_peculiarity_btn)
        modify_love_peculiarity_btn.snp.makeConstraints { (make) in
            make.width.equalTo(54)
            make.height.equalTo(24)
            make.left.equalToSuperview().offset(15)
            make.centerY.equalTo(add_btn)
        }
        
        binding()
    }
    
    private func binding(){
        
        SCComponets.login_status_did_changed_observable().distinctUntilChanged().withUnretained(self).subscribe(onNext: { own,f in
            print("SCComponets.login_status_did_changed_observable()")
            if f == true{
                own.refresh_data()
            }else{
                own.clear_data()
            }
            
        }).disposed(by: rx.disposeBag)
        
    }
    
    fileprivate var normal_selected_btn = false
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        if !normal_selected_btn{
            normal_selected_btn = true
            
            last_btn?.isSelected = false
            
            self.recommend_tab_btn.isSelected = true
            
            last_btn = self.recommend_tab_btn
            
            scrollView.setContentOffset(CGPoint.init(x: 1.cgFloat*ScreenWidth, y: 0), animated: false)
            
        }
    }
    
    
    func clear_data(){
        filterSEX = nil
    }
    
    @objc fileprivate func refresh_data(){
        set_recommend_btn_data()
    }
    
    
    fileprivate var filterSEX:ST_SEX?
    
    fileprivate func set_recommend_btn_data(){
        
        var selected_sex:ST_SEX?
        
        if let curr = filterSEX{
            selected_sex = curr
        }
        
        if selected_sex == nil{
            //            自己是男
            if ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex == ST_SEX.male{
                selected_sex = .female
            }else{
                selected_sex = .male
            }
        }
        
        filterSEX = selected_sex
        
        //        var str = "男生 "
        //
        //        if selected_sex == .female{
        //            str = "女生 "
        //        }
        
        //        recommend_tab_btn.setTitleForAllStates(str)
        
    }
    
    //    MARK: - properties
    fileprivate lazy var scrollView:UIScrollView = {
        let control = UIScrollView()
        control.backgroundColor = UIColor.white
        //        control.isScrollEnabled = false
        control.isPagingEnabled = true
        control.delegate = self
        return control
    }()
    
    
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.white
        return control
    }()
    
    
    fileprivate lazy var my_follow_controller = ST_PeculiarityListOfMyFollowController.shared
    
    
    fileprivate lazy var recommend_controller = ST_RecommendPeculiarityController.shared
    
    fileprivate lazy var peculiarity_squre_controller = ST_PeculiaritySquareController()
    
    fileprivate var last_btn:UIButton?
    
    fileprivate var baseTag = 10234
    
    fileprivate lazy var btn_selected_image:UIImage = {
        let img = UIImage.init(color: UIColor.init(hexString: "#FEC207")!, size: CGSize.init(width: 1, height: 1))
        return img
    }()
    
    fileprivate lazy var btn_normal_image:UIImage = {
        let img = UIImage.init(color: UIColor.white, size: CGSize.init(width: 1, height: 1))
        return img
    }()
    
    fileprivate lazy var follow_tab_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setBackgroundImage(btn_selected_image, for: UIControl.State.selected)
        control.setBackgroundImage(btn_normal_image, for: UIControl.State.normal)
        control.cornerRadius = 19
        control.setTitleForAllStates("喜欢")
        control.tag = baseTag
        control.setTitleColorForAllStates(UIColor.init(hexString: "#333333")!)
        control.setTitleColor(UIColor.white, for: UIControl.State.selected)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(click_switch_controller_handle(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var recommend_tab_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setBackgroundImage(btn_selected_image, for: UIControl.State.selected)
        control.setBackgroundImage(btn_normal_image, for: UIControl.State.normal)
        
        control.cornerRadius = 19
        control.semanticContentAttribute = .forceRightToLeft
        
        control.setImage(ST_ImageUtils.imageName("推荐_筛选_icon")!, for: UIControl.State.selected)
        
        control.setTitleForAllStates("推荐")
        
        control.tag = baseTag + 1
        control.setTitleColorForAllStates(UIColor.init(hexString: "#333333")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        control.setTitleColor(UIColor.white, for: UIControl.State.selected)
        control.addTarget(self, action: #selector(click_switch_controller_handle(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var lastIndex = 1
    
    
    
    
    fileprivate lazy var type_tab_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setBackgroundImage(btn_selected_image, for: UIControl.State.selected)
        control.setBackgroundImage(btn_normal_image, for: UIControl.State.normal)
        control.cornerRadius = 19
        control.setTitleForAllStates("分类")
        control.tag = baseTag+2
        control.setTitleColorForAllStates(UIColor.init(hexString: "#333333")!)
        control.setTitleColor(UIColor.white, for: UIControl.State.selected)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(click_switch_controller_handle(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var sign_label:UILabel = {
        let control = UILabel()
        control.text = "NEW"
        control.font = UIFont.systemFont(ofSize: 9, weight: UIFont.Weight.medium)
        control.textColor = .white
        control.textAlignment = .center
        control.backgroundColor = UIColor.init(hex: 0xEB3C3C)
        control.alpha = 1
        control.cornerRadius = 8
        return control
    }()
    
    
    //    MARK: - filter Controller
    
    fileprivate lazy var add_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("首页_icon_添加")!)
        control.addTarget(self, action: #selector(push_peculiarity(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    fileprivate lazy var modify_love_peculiarity_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setBackgroundImage(btn_selected_image, for: UIControl.State.normal)
        control.cornerRadius = 24/2.0
        control.setTitleForAllStates("偏好")
        control.setTitleColorForAllStates(UIColor.white)
        
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
        
        control.addTarget(self, action: #selector(push_followed_peculiarity_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var filter_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("首页_icon_筛选")!)
        control.addTarget(self, action: #selector(filter_gender_handle(sender:)), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    @objc func filter_gender_handle(sender:UIButton){
        
        
        let vc = ST_FilterFeedViewController()
        vc.overCurrentContext = true
        vc.over_current_context_alpha = 0.5.string
        vc.confirm_closure = {[weak self] gender in
            self?.recommend_controller.filter_remote_data(sex: gender)
            
            if gender == .male{
                //                self?.recommend_tab_btn.setTitleForAllStates("男生")
                
                self?.filterSEX = .male
                
            }else{
                //                self?.recommend_tab_btn.setTitleForAllStates("女生")
                self?.filterSEX = .female
            }
            
            self?.set_recommend_btn_data()
        }
        
        vc.normal_selected_sex = self.filterSEX
        
        GlobalPresent(toController: vc)
        
    }
    
    @objc func push_peculiarity(sender:UIButton){
        
        /* 取消发布日常
         let alert = ST_PushOptionViewController()
         
         alert.overCurrentContext = true
         
         alert.over_current_context_alpha = "0.4"
         
         GlobalPresent(toController: alert)
         */
        
        push_to_push_peculiarity_controller(navigationController: ST_BTLaunchController.shared.navigationController)
    }
}


extension ST_PeculiarityModuleViewController:UIScrollViewDelegate{
    
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        
        
        if !decelerate{
            scrollviewdidEndScroll(scrollView)
        }
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        scrollviewdidEndScroll(scrollView)
        
    }
    
    
    //    MARK: 停止滚动
    func scrollviewdidEndScroll(_ scrollView:UIScrollView){
        
        let page = (scrollView.contentOffset.x/ScreenWidth).int
        
        if page == lastIndex {
            return
        }
        
        lastIndex = page
        
        last_btn?.isSelected = false
        
        let sender = [self.follow_tab_btn,self.recommend_tab_btn,self.type_tab_btn][safe: page]
        
        sender?.isSelected = true
        
        last_btn = sender
        
        //        filter_btn.isHidden = sender != recommend_tab_btn
        modify_love_peculiarity_btn.isHidden = sender != recommend_tab_btn
        
    }
    
    @objc fileprivate func push_followed_peculiarity_handle(){
        
        
        let vc = ST_ModifyMyLikeTraitViewController()
        
        vc.refresh_closure = {[weak self] in
            self?.recommend_controller.refresh_data()
        }
        
        self.navigationController?.pushViewController(vc)
        
    }
    
    @objc func click_switch_controller_handle(sender:UIButton){
        
        if sender.isSelected{
            
            if sender == recommend_tab_btn{
                filter_gender_handle(sender: sender)
            }
            
            return
        }
        
        //        if sender == recommend_tab_btn{
        //            sign_label.alpha = 0
        //        }
        
        last_btn?.isSelected = false
        
        sender.isSelected = true
        
        last_btn = sender
        
        //        filter_btn.isHidden = sender != recommend_tab_btn
        modify_love_peculiarity_btn.isHidden = sender != recommend_tab_btn
        
        
        scrollView.setContentOffset(CGPoint.init(x: (sender.tag-self.baseTag).cgFloat*ScreenWidth, y: 0), animated: true)
        
    }
    
}
